/* $RCSFile: ADNDynTemplate.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynTemplate.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

/* Copyrights Christian Surieux 2010 */
/* Class ADNDynTemplate */
/* Date 2010-01-10 19:35:17Z */

using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public partial class CrmStr
	{
		public const string generationtypecode			=	"generationtypecode";
		public const string ispersonalname				=	"ispersonalname";
		public const string presentationxml				=	"presentationxml";
		public const string subjectpresentationxml	    =	"subjectpresentationxml";
		public const string templateid				    =	"templateid";
	}

	public class ADNDynTemplate : DynEntity
	{
		/* Internal class for DynTypes */
		protected class Ftor: DFactor
		{
			public Ftor() : base(CrmStr.template){}
			public override Type DynType
			{	get{return typeof(ADNDynTemplate);}}
			public override string DynTypename
			{	get{return CrmStr.template;}}
			public override DynEntity Build()
			{	return new ADNDynTemplate();}
			public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
			{	return new ADNDynTemplate(w, dEntity, wproperties, null);}
			public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
			{	return new ADNDynTemplate(w, PrimaryFieldValue);}
			public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
			{	return new ADNDynTemplate(w,PrimaryKeyValue);}
		}
		/* Static Ctor to register in DynTypes */
		static ADNDynTemplate(){new Ftor();}

		public override bool HasOwningBusinessUnitProperty
		{	get{ return true;}}
		public ADNDynTemplate(): base(){}
		public ADNDynTemplate(CRMWrapperBase w, string PrimaryFieldValue): base(w, CrmStr.template)
		{
			if ( PrimaryFieldValue != string.Empty )
			title	=   PrimaryFieldValue;
		}
		public ADNDynTemplate(CRMWrapperBase w, Guid CrmId): base(w,CrmStr.template, CrmId){}
		public ADNDynTemplate(CRMWrapperBase w, InternalDynamicEntity e): base(w,CrmStr.template,e,null,null){}
		public ADNDynTemplate(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(w,CrmStr.template,e,prop,s){}
		public ADNDynTemplate(ResetWrapperHandler fn, string PrimaryFieldValue): base(fn,CrmStr.template)
		{
			if ( PrimaryFieldValue != string.Empty )
				title	=   PrimaryFieldValue;
		}
		public ADNDynTemplate(ResetWrapperHandler fn, Guid CrmId): base(fn,CrmStr.template,CrmId){}
		public ADNDynTemplate(ResetWrapperHandler fn,InternalDynamicEntity e): base(fn,CrmStr.template,e,null,null){}
		public ADNDynTemplate(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s): base(fn,CrmStr.template,e,prop,s){}

		/* Properties */

		[CrmDynAttribute(CrmStr.template)]
		public Guid templateid
		{
		get	{	 return KeyPropertyValue(CrmStr.templateid);}
		set	{	AddKeyProperty(CrmStr.templateid, value);}
		}
		protected override void SetCrmIdProperty(Guid crmId)
		{
			if ( !dicProperty.ContainsKey(CrmStr.templateid)  || KeyPropertyValue(CrmStr.templateid) != crmId )
				templateid	=   crmId;
		}
		public override Guid PrimaryKeyGuidValue
		{
		    get	{	return templateid;}
		    set{	templateid	=value;}
		}
		
        public override string PrimaryFieldName
		{
		    get	{	return CrmStr.title;}
		    set	{}
		}

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return title;
            }
            set
            {
                title =   value;
            }
        }

		[CrmDynAttribute(CrmStr.template)]
		public string title
		{
		    get	{	 return StringPropertyValue(CrmStr.title);}
		    set	{	AddStringProperty(CrmStr.title, value);}
		}

        [CrmDynAttribute(CrmStr.template)]
		public string body
		{
		    get	{	 return StringPropertyValue(CrmStr.body);}
		    set	{	AddStringProperty(CrmStr.body, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string description
		{
		    get	{	 return StringPropertyValue(CrmStr.description);}
		    set	{	AddStringProperty(CrmStr.description, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public int generationtypecode
		{
		    get	{	 return NumberPropertyValue(CrmStr.generationtypecode);}
		    set	{	AddNumberProperty(CrmStr.generationtypecode, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public bool ispersonal
		{
			get	{	 return BooleanPropertyValue(CrmStr.ispersonal);}
		    set	{	AddBooleanProperty(CrmStr.ispersonal, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public int languagecode
		{
		    get	{	 return NumberPropertyValue(CrmStr.languagecode);}
		    set	{	AddNumberProperty(CrmStr.languagecode, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string mimetype
		{
		    get	{	 return StringPropertyValue(CrmStr.mimetype);}
		    set	{	AddStringProperty(CrmStr.mimetype, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public Guid owningbusinessunit
		{
		    get	{	 return LookupPropertyValue(CrmStr.owningbusinessunit);}
		    set	{	AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);}
		}

		public string businessunitName
		{
    		get	{	 return LookupPropertyName(CrmStr.owningbusinessunit);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public Guid owningteam
		{
		    get	{	 return LookupPropertyValue(CrmStr.owningteam);}
		    set	{	AddLookupProperty(CrmStr.owningteam,CrmStr.team,value);}
		}

		public string teamName
		{
    		get	{	 return LookupPropertyName(CrmStr.owningteam);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public Guid owninguser
		{
		    get	{	 return LookupPropertyValue(CrmStr.owninguser);}
		    set	{	AddLookupProperty(CrmStr.owninguser,CrmStr.systemuser,value);}
		}

		public string systemuserName
		{
    		get	{	 return LookupPropertyName(CrmStr.owninguser);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string presentationxml
		{
		    get	{	 return StringPropertyValue(CrmStr.presentationxml);}
		    set	{	AddStringProperty(CrmStr.presentationxml, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string subject
		{
		    get	{	 return StringPropertyValue(CrmStr.subject);}
		    set	{	AddStringProperty(CrmStr.subject, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string subjectpresentationxml
		{
		    get	{	 return StringPropertyValue(CrmStr.subjectpresentationxml);}
		    set	{	AddStringProperty(CrmStr.subjectpresentationxml, value);}
		}

		[CrmDynAttribute(CrmStr.template)]
		public string templatetypecode
		{
		    get	{	 return EntityNameReferencePropertyValue(CrmStr.templatetypecode);}
		    set	{	AddEntityNameReferenceProperty(CrmStr.templatetypecode,value);}
        }

        #region utilities

        public ADNDynEmail InstantiateTemplate(DynEntity dyn,bool isAdmin)
        {
            return wrapper.Util.InstantiateTemplate(this,dyn,isAdmin);
        }

        public void SendTemplate(List<DynEntity> dynLst,DynEntity Regarding,ADNDynSystemUser user,bool isAdmin)
        {
            wrapper.Util.SendTemplate(this,dynLst,Regarding,user,isAdmin);
        }

        /// <summary>
        /// Build a SortedList 
        /// templateType: string.empty for all entities, else the entity
        /// lancode: -1 all, else the language code
        /// </summary>
        static public SortedList<Guid,ADNDynTemplate> ReadAll(CRMWrapperBase wrapper,ResetWrapperHandler fn,string templatetype,int langcode,bool isAdmin)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]ADNDynTemplate ReadAll BEG.",(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            SortedList<Guid,ADNDynTemplate> Lst      =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.template;
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                List<InternalConditionExpression> cLst          =   new List<InternalConditionExpression>();
                if ( !string.IsNullOrEmpty(templatetype))
                {
                    InternalConditionExpression ex              =   new InternalConditionExpression();
                    ex.AttributeName                            =   CrmStr.templatetypecode;
                    ex.Operator                                 =   InternalConditionOperator.Equal;
                    ex.Values                                   =   new string[]{templatetype};
                    cLst.Add(ex);
                }
                if ( langcode != -1 )
                {
                    InternalConditionExpression ex              =   new InternalConditionExpression();
                    ex.AttributeName                            =   CrmStr.languagecode;
                    ex.Operator                                 =   InternalConditionOperator.Equal;
                    ex.Values                                   =   new object[]{langcode};
                    cLst.Add(ex);
                }
                if ( cLst.Count > 0 )
                    fi.Conditions                               =   cLst.ToArray();

                qe.ColumnSet                                    =   new InternalAllColumns();
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynTemplate> allEntQuery      =   new GenericDynQueryMultiple<ADNDynTemplate>(wrapper,fn,CrmStr.template);
                allEntQuery.isAdmin                             =   isAdmin;
                Lst                                             =   allEntQuery.retrieveSortedList(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynTemplate ReadAll ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynTemplate ReadAll END Count: {0}",Lst.Count),(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Lst;
        }

        /// <summary>
        /// Build a DataTable
        /// templateType: string.empty for all entities, else the entity
        /// lancode: -1 all, else the language code
        /// </summary>
        static public DataTable ReadAllTb(CRMWrapperBase wrapper,ResetWrapperHandler fn,string templatetype,int langcode,bool isAdmin)
        {
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]ADNDynTemplate ReadAllTb BEG.",(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            DataTable Tb                                        =   null;
            try
            {
                InternalQueryExpression qe                      =   new InternalQueryExpression();
                qe.EntityName                                   =   CrmStr.template;
                InternalFilterExpression fi                     =   new InternalFilterExpression();
                List<InternalConditionExpression> cLst          =   new List<InternalConditionExpression>();
                if ( !string.IsNullOrEmpty(templatetype))
                {
                    InternalConditionExpression ex              =   new InternalConditionExpression();
                    ex.AttributeName                            =   CrmStr.templatetypecode;
                    ex.Operator                                 =   InternalConditionOperator.Equal;
                    ex.Values                                   =   new string[]{templatetype};
                    cLst.Add(ex);
                }
                if ( langcode != -1 )
                {
                    InternalConditionExpression ex              =   new InternalConditionExpression();
                    ex.AttributeName                            =   CrmStr.languagecode;
                    ex.Operator                                 =   InternalConditionOperator.Equal;
                    ex.Values                                   =   new object[]{langcode};
                    cLst.Add(ex);
                }
                if ( cLst.Count > 0 )
                    fi.Conditions                               =   cLst.ToArray();

                qe.ColumnSet                                    =   new InternalAllColumns();
                qe.Criteria                                     =   fi;
                GenericDynQueryMultiple<ADNDynTemplate> allEntQuery      =   new GenericDynQueryMultiple<ADNDynTemplate>(wrapper,fn,CrmStr.template);
                allEntQuery.isAdmin                             =   isAdmin;
                Tb                                              =   allEntQuery.retrieveDatatable(qe);
            }
            catch (Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynTemplate ReadAllTb ex: {0}", ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
            if (CRMDynTraceSwitch.Sw.TraceVerbose || CRMDynTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynTemplate ReadAllTb END Count: {0}",(Tb != null) ? Tb.Rows.Count:0),(CRMDynTraceSwitch.Sw.TraceVerbose)? CRMDynTraceSwitch.Sw.Info:CRMDynTraceSwitch.Sw.Info);
            return Tb;
        }
        #endregion utilities
    }
}